package xian.woniuxy.be.web;

import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xian.woniuxy.be.entity.Contactperson;
import xian.woniuxy.be.entity.ContactpersonExample;
import xian.woniuxy.be.entity.Customer;
import xian.woniuxy.be.entity.ResultVO;
import xian.woniuxy.be.service.IContactpersonService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong
 * @time 2022/01/09 00:35:21
 */
@RestController
public class ContactpersonController {
    @Autowired
    private IContactpersonService contactpersonService;

    @PostMapping("contactperson/save")
    public ResultVO save(@RequestBody Contactperson contactperson){

        if (contactperson.getCusId() == null){
            return new ResultVO(501, "客户不能为空", null);
        }
        if (contactperson.getConName() == ""){
            return new ResultVO(502, "名字不能为空", null);
        }
        if (contactperson.getConGender() == null){
            return new ResultVO(503, "性别不能为空", null);
        }
        if (contactperson.getConPosition() == ""){
            return new ResultVO(504, "职位不能为空", null);
        }


        contactpersonService.save(contactperson);
        return ResultVO.success("操作成功",null);
    }

    @PostMapping("contactperson/delete")
    public ResultVO delete(@RequestBody String conId){
        if (!StringUtils.hasText(conId)){
            throw new RuntimeException("联系人ID为空");
        }
        contactpersonService.delete(Integer.valueOf(conId));
        return ResultVO.success("删除成功");
    }

    @PostMapping("contactperson/update")
    public ResultVO update(@RequestBody Contactperson contactperson){

        if (contactperson.getCusId() == null){
            return new ResultVO(501, "客户不能为空", null);
        }
        if (contactperson.getConName() == ""){
            return new ResultVO(502, "名字不能为空", null);
        }
        if (contactperson.getConGender() == null){
            return new ResultVO(503, "性别不能为空", null);
        }
        if (contactperson.getConPosition() == ""){
            return new ResultVO(504, "职位不能为空", null);
        }

        contactpersonService.update(contactperson);
        return ResultVO.success("修改成功");
    }

    @PostMapping("contactperson/findAll")
    public ResultVO findAll(){
        List<Contactperson> list = contactpersonService.findAll();
        return ResultVO.success(list);
    }

    @PostMapping("contactperson/findOne")
    public ResultVO findOne(@RequestBody String cusId){
        if (!StringUtils.hasText(cusId)){
            throw new RuntimeException("联系人ID为空");
        }
        List<Contactperson> list = contactpersonService.findOne(Integer.valueOf(cusId));
        return ResultVO.success(list);
    }

    @PostMapping("contactperson/selectFromname")
    public ResultVO selectByName(@RequestBody String theName){
        List<Contactperson> list = new ArrayList<>();
        if (!StringUtils.hasText(theName)){
            list = contactpersonService.findAll();
        }
        list = contactpersonService.selectFromname(theName);
        return ResultVO.success(list);
    }
}
